﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Surveillance
{
    class Alarm : Switchable
    {
        #region alarm declarations

        public static Texture2D alarmTexture;
        private bool isOn;
        private Rectangle targetLocation;
        private Rectangle sourceFrame;
        private Enemy effectedEnemy;
        List<Vector2> alarmWaypoints;
        List<Vector2> enemyWaypoints;
        float timePassed;

        #endregion

        #region alarm constructors

        public Alarm(Rectangle target, Enemy enemy, List<Vector2> waypoints)
        {
            targetLocation = target;
            sourceFrame = new Rectangle(0, 0, 30, 30);
            timePassed = 0f;
            effectedEnemy = enemy;
            alarmWaypoints = waypoints;
            enemyWaypoints = enemy.WayPoints;
            isOn = false;
        }

        #endregion

        #region alarm public functions

        //mode:
        //0 - toggle
        //1 - off
        //2 - on
        public override void doSwitch(int mode)
        {
            switch (mode)
            {
                case 0: isOn = !isOn; break;
                case 1: isOn = false; break;
                case 2: isOn = true; break;
            }
            if (isOn)
            {
                effectedEnemy.WayPoints = alarmWaypoints;
            }
            else
            {
                effectedEnemy.WayPoints = enemyWaypoints;
                sourceFrame.X = 0;
            }
        }

        public void reset()
        {
            isOn = false;
            sourceFrame.X = 0;
        }

        #endregion

        #region alarm update and draw

        public void Upadte(GameTime gameTime)
        {
            if (isOn)
            {
                timePassed += gameTime.ElapsedGameTime.Milliseconds;
                if (timePassed > 500.0f)
                {
                    //time to change frame
                    timePassed = 0f;
                    sourceFrame.X = (sourceFrame.X + 30) % 60;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
         spriteBatch.Draw(alarmTexture, targetLocation, sourceFrame, Color.White);  
        }

    #endregion
    }

    
}
